//#include <mh/app.hpp>
#include <mh/audio/dsound.hpp>

namespace Mh
{
	extern ::HWND g_hwnd;
	
	namespace Audio
	{
		// 이하 BufferDSound 클래스 정의
		BufferDS::BufferDS()
		{}

		BufferDS::~BufferDS()
		{}

		// 이하 ListenerDSound 클래스 정의
		ListenerDS::ListenerDS()
			:	m_listener( NULL )
		{}

		ListenerDS::~ListenerDS()
		{}

		// 이하 SystemDSound 클래스 정의
		SystemDS::SystemDS()
			:	System( TYPE_DSOUND ), m_device( NULL )
		{}

		SystemDS::~SystemDS()
		{
			finalize();
		}

		bool SystemDS::initialize()
		{
			HRESULT hr;

			// 라이브러리 링크
			{
				m_module = LoadLibrary( TEXT("dsound.dll") );
				if( !m_module )
					return false;
			}
						
#if 0
			// DirectSound 장치 열거
			hr = ::DirectSoundEnumerate();
			if( FAILED(hr) )
				return false;
#endif
			
			// DirectSound 장치 작성
			{
				typedef HRESULT (WINAPI *DSoundCreateFunc)( ::LPCGUID, ::LPDIRECTSOUND8*, ::LPUNKNOWN);
				DSoundCreateFunc func = (DSoundCreateFunc)::GetProcAddress( m_module, "DirectSoundCreate8" );
				if( !func )
					return false;

				hr = (*func)( NULL, &m_device, NULL );
				if( FAILED(hr) )
					return false;
			}

			m_device->SetCooperativeLevel( g_hwnd, DSSCL_NORMAL );
		
			this->get_caps();
			this->get_speaker_info();
			
			return true;
		}

		void SystemDS::finalize()
		{
			SafeRelease( &m_device );
			if( m_module )
			{
				::FreeLibrary( m_module );
				m_module = NULL;
			}
		}

		void SystemDS::process()
		{
		}

		void SystemDS::suspend()
		{
		}

		void SystemDS::get_caps()
		{
			m_caps.dwSize = sizeof(::DSCAPS);
			HRESULT hr = m_device->GetCaps( &m_caps );
			if( SUCCEEDED(hr) )
			{
				
			}
		}

		void SystemDS::get_speaker_info()
		{
			HRESULT hr;
			::DWORD speaker;
			hr = m_device->GetSpeakerConfig( &speaker );
			if( SUCCEEDED(hr) )
			{
				switch( DSSPEAKER_CONFIG(speaker) )
				{
				case DSSPEAKER_DIRECTOUT:
					break;
				case DSSPEAKER_HEADPHONE:
					break;
				case DSSPEAKER_MONO:
					break;
				case DSSPEAKER_QUAD:
					break;
				case DSSPEAKER_STEREO:
					switch( DSSPEAKER_GEOMETRY(speaker) )
					{
					case DSSPEAKER_GEOMETRY_MIN:
						break;
					case DSSPEAKER_GEOMETRY_NARROW:
						break;
					case DSSPEAKER_GEOMETRY_WIDE:
						break;
					case DSSPEAKER_GEOMETRY_MAX:
						break;
					}
					break;
				case DSSPEAKER_SURROUND:
					break;
				case DSSPEAKER_7POINT1_WIDE:
					break;
				case DSSPEAKER_7POINT1_SURROUND:
					break;
				case DSSPEAKER_5POINT1_BACK:
					break;
				case DSSPEAKER_5POINT1_SURROUND:
					break;
				}
			}
		}

	}	// namespace Mh::Audio
}		// namespace Mh